package com.jliang.apps.topic;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class TopicReceiver {
	//ssp.queue: ssp.console.topic.notify${ssp.machine_no}
	@Value("${ssp.queue}")
	private String queueName;

	@RabbitListener(
            bindings = @QueueBinding(value = @Queue(value = "${ssp.queue}", durable = "true"),
                    exchange = @Exchange(value = "sspTopicExchange", durable = "true", type = "topic", ignoreDeclarationExceptions = "true"), key = "ssp.console.topic.routekey")
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        log.info(queueName+"--消费端Payload1: " + message.getPayload());
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        System.out.println("传递的类型  : " + message.getPayload());
        log.info("deliveryTag1: " + deliveryTag);
        String type = String.valueOf(message.getPayload());
        if("app".equals(type)){
        	System.out.println("刷新应用缓存");
        }else if("mch".equals(type)){
        	System.out.println("刷新商户配置");
        }else if("product".equals(type)){
        	System.out.println("刷新产品配置");
        }else{
        	System.out.println("其它");
        }
        channel.basicAck(deliveryTag, false);
    }

}
